﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using System.Text;

public partial class MyLy_MylyBuycard : System.Web.UI.Page
{
    //手机号
    public string Mobile
    {
        get
        {   
            return ViewState["mobile"] != null ? ViewState["mobile"].ToString() : "";
        }
        set { ViewState["mobile"] = value; }
    }


    public int TotalCount
    {
        get 
        {
            return ViewState["totalCount"] != null ? Convert.ToInt32(ViewState["totalCount"].ToString()) : 0;
        }
        set { ViewState["totalCount"] = value.ToString(); }
    }

    //页码
    public int PageIndex
    {
        get
        {
            return ViewState["pageIndex"] != null ? Convert.ToInt32(ViewState["pageIndex"].ToString()) : 0;
        }
        set { ViewState["pageIndex"] = value.ToString(); }
    }
    //循环次数
    public int LoopTimes 
    {
        get
        {
            return ViewState["loopTimes"] != null ? Convert.ToInt32(ViewState["loopTimes"].ToString()) : 0;
        }
        set { ViewState["loopTimes"] = value.ToString(); }
    }

    //查询条件
    public string WhereCondition
    {
        get
        {
            return ViewState["WhereCondition"] != null ? ViewState["WhereCondition"].ToString() : "";
        }
        set { ViewState["WhereCondition"] = value; }
    }

    BLL.PagingControl paging = new PagingControl();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginUser"] == null)
        {
            Response.Redirect(WebUtility.getWebRootPath() + "/Login/Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            this.Txt_StartTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
            this.Txt_EndTime.Attributes.Add("onfocus", "WdatePicker({skin:'ext',dateFmt:'yyyy-MM-dd HH:mm:ss'})");
            this.Mobile = Session["LoginUser"].ToString();
            WhereCondition = "S_MobileNO =" + "'" + this.Mobile + "'";
            //设置起始页
            this.PageIndex = 1;
            this.bind(1, WhereCondition);
        }
    }

    //按日期查询
    protected void ImgBtn_Query_Click(object sender, ImageClickEventArgs e)
    {
        string startTime = WebUtility.replaceStr(this.Txt_StartTime.Text.Trim());
        string endTime = WebUtility.replaceStr(this.Txt_EndTime.Text.Trim());
        StringBuilder buider = new StringBuilder();
        this.WhereCondition = "S_MobileNO =" + "'" + this.Mobile + "'";
        buider.Append(this.WhereCondition + " and S_Time between '{0}' and '{1}'");
        string sql = string.Format(buider.ToString(),startTime,endTime);
        this.WhereCondition = sql;
        this.PageIndex = 1;
        this.bind(PageIndex,sql);
    }

    /// <summary>
    /// 构造新的Datatable
    /// </summary>
    private DataTable buildNewTable(DataTable table)
    {
        //S_CardNO S_CardPoints S_PayMoney S_PayType S_Time State
        // 2009年12月30日23:51:25 附加状态栏
        //DataColumn S_CardNO = new DataColumn("S_CardNO", System.Type.GetType("System.String"));
        //DataColumn S_CardPoints = new DataColumn("S_CardPoints", System.Type.GetType("System.String"));
        //DataColumn S_PayMoney = new DataColumn("S_PayMoney", System.Type.GetType("System.String"));
        //DataColumn S_PayType = new DataColumn("S_PayType", System.Type.GetType("System.String"));
        //DataColumn S_Time = new DataColumn("S_Time", System.Type.GetType("System.String"));
        DataColumn State = new DataColumn("State", System.Type.GetType("System.String"));
        //newTable.Columns.Add(S_CardNO);
        //newTable.Columns.Add(S_CardPoints);
        //newTable.Columns.Add(S_PayMoney);
        //newTable.Columns.Add(S_PayType);
        //newTable.Columns.Add(S_Time);
        table.Columns.Add(State);
        BLL.CardsInfo cardInfo = new CardsInfo();
           for (int k = 0; k < table.Rows.Count;k++ )
        {
           DataRow row = table.Rows[k];
           //根据卡号获取卡的状态
           string cardNo = row["S_CardNO"].ToString();
           if (!String.IsNullOrEmpty(cardNo))
           {
               string state = cardInfo.getCardState(cardNo);
               if (String.IsNullOrEmpty(state))
               {
                   row["State"] = state;
               }
               else {
                  // row["State"] = "未使用";
				  row["State"] = " ";
               }
           }
        }
        return table;
    }

    //绑定数据
    public void bind(int pageIndex, string whereCondition)
    {
        //string tableName, string SortFieldName, string showFieldNames, int pageSize, int pageIndex, int orderType, string whereCondition
        string tableName = "PurchasedCardHistroy";
        string SortFieldName = "S_Time";
        DataTable table = paging.GetData(tableName, SortFieldName, "*", 10, pageIndex, 1, whereCondition);
        table = buildNewTable(table);
        this.Grid_PurchaseHistroy.DataSource = table.DefaultView;
        this.Grid_PurchaseHistroy.DataBind();
        //获取总条数
        this.TotalCount = paging.getTotalCount();
        //---------------------
        this.ddlCurrentPage.Items.Clear();
        if (TotalCount < 10)
        {
            this.ddlCurrentPage.Items.Add("1");
        }
        else
        {
            //----------------
            int loopTimes = 0;
            //判断取模后是否大于0
            if (TotalCount % 10 > 0)
            {
                loopTimes = TotalCount / 10 + 1;
            }
            else 
            {
                loopTimes = TotalCount / 10;
            }

            this.LoopTimes = loopTimes;
            //----------------------
            for (int i = 1; i <= loopTimes; i++)
            {
                this.ddlCurrentPage.Items.Add(i.ToString());
            }
        }
    }

  
    /// <summary>
    /// 返回选中的页码
    /// </summary>
    /// <returns></returns>
    public int getSelectedDropDownListValue()
    {
        int k = Convert.ToInt32(this.ddlCurrentPage.SelectedItem.Value);
        return k;
    }
    //---------------------------------
    /// <summary>
    /// 选择第几页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DdlCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.PageIndex = this.getSelectedDropDownListValue();
        bind(PageIndex, WhereCondition);
    }
    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkbtnFrist_Click(object sender, EventArgs e)
    {
        PageIndex = 1;
        this.lnkbtnPre.Enabled = false;
        this.lnkbtnNext.Enabled = true;
        bind(1, WhereCondition);
    }
    /// <summary>
    ///前一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkbtnPre_Click(object sender, EventArgs e)
    {
        if (PageIndex > 1)
        {
            PageIndex = PageIndex - 1;
            this.lnkbtnPre.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            bind(PageIndex, WhereCondition);
        }
        else
        {
            this.lnkbtnPre.Enabled = false;
            this.lnkbtnNext.Enabled = true;
            bind(PageIndex, WhereCondition);
        }
    }
    /// <summary>
    /// 下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        if (PageIndex < LoopTimes)
        {
            PageIndex = PageIndex + 1;
            this.lnkbtnPre.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            bind(PageIndex, WhereCondition);
        }
        else
        {
            this.lnkbtnPre.Enabled = true;
            this.lnkbtnNext.Enabled = false;
            bind(PageIndex, WhereCondition);
        }
    }
    /// <summary>
    /// 最后一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnkbtnLast_Click(object sender, EventArgs e)
    {
        PageIndex = LoopTimes;
        if (PageIndex <= 0)
        {
            PageIndex = 1;
        }
        this.lnkbtnPre.Enabled = true;
        this.lnkbtnNext.Enabled = false;
        bind(PageIndex, WhereCondition);
    }
    //---------------------------------
}
